/*River Warren
 *October 10, 2011
 *
 *Updated Oct 14 2011, 12:21, remove itoa, replace with sprintf
 *Updated Nov 5, 2011, River Warren
 *Castle.cpp
*/

#include "Castle.h"
#include <algorithm>

//default constructor
//for new game sets current room to 0
Castle::Castle(string dir)
{
    loadRooms(dir);//load rooms into array
}

Castle::~Castle(){}

//load rooms from files into array
void Castle::loadRooms(string dir)
{
    string str;
    char buffer [33];
    for(int i = 0; i <= NUMOFROOMS; ++i)
    {
        Room r;
        std::sprintf(buffer, "%d", i);
        str = 'R';
        str += buffer;
        r.loadRoom(dir + '/' + str);
        rooms.push_back(r);
    }
}

Room * Castle::getRoom(string roomID) {

    Room * roomPtr;

    vector<Room>::iterator roomIT;

    roomIT = find(rooms.begin(), rooms.end(), roomID);

    roomPtr = &(*roomIT);
    return roomPtr;
}
//saves current state of rooms
//right now saves to working directory
//should have path string as parameter
void Castle::saveRooms(string dir)
{
    for(int i = 0; i <= NUMOFROOMS; ++i)
    {
        try
        {
            rooms[i].saveRoom(dir);
        }
        catch(string msg)
        {
            throw msg;
        }
    }
}
